<?php

namespace app\wechat\home;

use app\common\controller\Common;

use app\wechat\model\Area as AreaModel;

use app\wechat\model\Seat as SeatModel;

use app\wechat\model\Reservation as ReservationModel;

use app\wechat\model\User as UserModel;

//预约订座控制器

class Reservation extends Common

{

    /**

     * 添加预约

     * @param  string $stime 初始时间

     * @param  string $etime 结束时间

     * @param  int $seatid 座位id

     * @param  int $userid 用户id

     * @param  string $formid 小程序表单id

     */

	public function add(){

    	$data = input('post.');

        $result = $this->validate($data, 'reservation.add');

        if($result !== true) {

            return show(0,$result);

        }

        $stime = strtotime($data['stime']);

        $etime = strtotime($data['etime']);

        $seatid = $data['seatid'];

        $userid = $data['userid'];

        $formid = $data['formid'];

    	$reservation = new ReservationModel();

        $user = new UserModel();

        $seat = new SeatModel();

        //获取权限

        $userFindMap['id'] = $userid;

        $userArr = $user->getOne($userFindMap);

        $group = $userArr['group'];

        //预约次数

        $dayNum = config('module_library.day_num');

        $todayCount = $reservation->getTodayCount($userid);

        if ($group != 2 && $todayCount >= $dayNum){

            return show(0,'您已超出每日预约次数'.$dayNum.'次，今天无法预约');

        }

        //座位判断

        $seatRes = $reservation->isReservation($seatid,$stime,$etime);

        if ($seatRes) {

            return show(0,'该座位已经预定，无法预约');

        }

        //座位信息

        $seatInfo = $seat->getSeatInfo($seatid);

        if ($group == 0 && $seatInfo['seatType'] == 4){

            return show(0,'交流间只能老师和管理员预约，您无法预约');

        }

        //人物判断

        $userRes = $reservation->isUserTime($userid,$stime,$etime);

        if ($userRes && $group != 2) {

            return show(0,'您在该时间段已经存在预约，请您预约其它时间段');

        }

        $addData['areaid'] = $seatInfo['areaid'];

        $addData['seatNum'] = $seatInfo['seatNum'];

        $addData['seatType'] = $seatInfo['seatType'];

        $addData['formid'] = $formid;

        $addData['startTime'] = $stime;

        $addData['endTime'] = $etime;

        $addData['seatid'] = $seatid;

        $addData['userid'] = $userid;

        $addRes = $reservation->updateData($addData);

    	if ($addRes) {

            return show(1,'预约成功');

        }

        return show(0,'预约失败,请重新预约');

	}

    /**

     * 签到页面信息

     * @param  string $userid 用户id

     * @param  string $areaid 区域id

     * @param  int $seatNum 座位数量

     */

    public function signinInfo(){

        $map = input('post.');

        $result = $this->validate($map, 'reservation.signinInfo');

        if($result !== true) {

            return show(0,$result);

        }

        $reservation = new ReservationModel();

        $area = new AreaModel();

        $time = time();

        $map['startTime'] = ['<=',$time];

        $map['endTime'] = ['>=',$time-10*60];

        $map['status'] = ['<>',1];

        $info = $reservation->getOne($map);

        if ($info) {

            $data['id'] = $info['id'];

            $data['seatNum'] = $info['seatNum'];

            $areaName = $area->getAllAreaName();

            $data['areaname'] = $areaName[$info['areaid']];

            switch ($info['seatType']) {

                case '1': $seatTypeName = '硬座';break;

                case '2': $seatTypeName = '软座';break;

                case '3': $seatTypeName = '学生仓';break;

                case '4': $seatTypeName = '交流间';break;

                default:$seatTypeName = '暂无';; break;

            }

            $data['seatType'] = $seatTypeName;

            $data['startTime'] = date('Y-m-d H:i',$info['startTime']);

            $data['endTime'] = date('Y-m-d H:i',$info['endTime']);

            $data['eTime'] = date('H:i',$info['endTime']);

            $data['signoff'] = config('module_library.signoff');

            $etime = $info['endTime'];

            if ($info['status'] == 2) {
                if (empty($data['signoff'])) {
                    $data['status'] = 6;
                }else{
                    if($etime-600 > $time){
                        $data['status'] = 6;
                    }elseif($etime-600 <= $time && $time <= $etime+600){
                        $data['status'] = 7;
                    }else{
                        $data['status'] = 3;
                    }
                }
            }else{
                $data['status'] = $info['status'];
            }
            return show(1,'当前时间您有该座位的预约',$data);

        }

        return show(0,'当前时间您没有该座位的预约，请仔细查看预约订单');

    }    

    /**

     * 签到/离场/签退

     * @param  int $id 预约表单id

     */

    public function signin(){

        $data = input('post.');

        $result = $this->validate($data, 'reservation.signin');

        if($result !== true) {

            return show(0,$result);

        }

        $reservation = new ReservationModel();

        $info = $reservation->getOne($data);

        $time = time();

        if ($info['status'] == 0) {

            $updatedata['status'] = 2;

            $updateRes = $reservation->updateData($updatedata,$data);

            if ($updateRes) {

                return show(1,'签到成功');

            }

            return show(0,'签到失败');

        }elseif ($info['status'] == 1) {

            return show(0,'您已取消预约');

        }elseif ($info['status'] == 2) {

            $signoff = config('module_library.signoff');

            if (empty($signoff)){

                $updatedata['status'] = 4;

                $updatedata['leaveTime'] = $time;

                $updateRes = $reservation->updateData($updatedata,$data);

                if ($updateRes) {

                    return show(1,'提前离场成功');

                }

                return show(0,'提前离场失败');

            }else{

                $etime = $info['endTime'];

                if($etime-600 > $time){

                    $updatedata['status'] = 4;

                    $updatedata['leaveTime'] = $time;

                    $updateRes = $reservation->updateData($updatedata,$data);

                    if ($updateRes) {

                        return show(1,'提前离场成功');

                    }

                    return show(0,'提前离场失败');

                }elseif($etime-600 <= $time && $time <= $etime+600){

                    $updatedata['status'] = 5;

                    $updateRes = $reservation->updateData($updatedata,$data);

                    if ($updateRes) {

                        return show(1,'签退成功');

                    }

                    return show(0,'签退失败');

                }else{

                    return show(0,'您未签退，已经违规');

                }

            }

        }elseif ($info['status'] == 3) {

            return show(0,'您已违规，超出签到时间');

        }elseif ($info['status'] == 4) {

            return show(0,'您已提前离场，请重新预约座位');

        }

        return show(0,'您的预约订单不存在');

    }

    /**

     * 获取该用户所有预约

     * @param  int $userid 用户id

     */

    public function allList(){

        $data = input('post.');

        $result = $this->validate($data, 'reservation.allList');

        if($result !== true) {

            return show(0,$result);

        }

        $page = !empty($data['page'])?$data['page']:1;

        switch ($data['type']) {

            case '1':$data['status'] = 0;break;

            case '2':$data['status'] = 3;break;

            default:;break;

        }

        unset($data['page'],$data['type']);

        $reservation = new ReservationModel();

        $area = new AreaModel();

        $list = $reservation->getAll($data,$page);

        $listData = $list['data'];

        $areaName = $area->getAllAreaName();

        foreach ($listData as $key => $value){

             $listData[$key]['startTime'] = date('Y-m-d H:i',$value['startTime']);

             $listData[$key]['endTime'] = date('Y-m-d H:i',$value['endTime']);

             $listData[$key]['eTime'] = date('H:i',$value['endTime']);

             $listData[$key]['areaname'] = $areaName[$value['areaid']];

            switch ($value['seatType']) {

                case '1': $seatTypeName = '硬座';break;

                case '2': $seatTypeName = '软座';break;

                case '3': $seatTypeName = '学生仓';break;

                case '4': $seatTypeName = '交流间';break;

                default:$seatTypeName = '暂无';; break;

            }

            $listData[$key]['seatType'] = $seatTypeName;

        }

        $list['data'] = $listData;

        return $list;

    }



    /**

     * 用户取消预约

     * @param  int $id 预约id

     */

    public function cancel(){

        $map = input('post.');

        $result = $this->validate($map, 'reservation.cancel');

        if($result !== true) {

            return show(0,$result);

        }

        $reservation = new ReservationModel();

        $info = $reservation->getOne($map);

        $time = time();

        if ($time+25*60 > $info['startTime']){

            return show(0,'距离预约开始时间小于25分钟，不能取消');

        }

        if ($info['status'] == 0){

            $data['status'] = 1;

            $res = $reservation->updateData($data,$map);

            if ($res){

                return show(1,'预约取消成功');

            }

            return show(0,'预约取消失败');

        }

        return show(0,'参数错误');

    }







}